package com.geeklan.base.utils;

import android.content.Context;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class UsbUtils {
    public static List<UsbDevice> getUsbDevices(Context context){
        List<UsbDevice> usbDeviceList = new ArrayList<>();
        UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
        for (UsbDevice device : usbDevices.values()) {
            if (device.getInterfaceCount() > 0) {
                UsbInterface usbInterface = device.getInterface(0);
                // 判断外设类型
                switch (usbInterface.getInterfaceClass()) {
                    case UsbConstants.USB_CLASS_MASS_STORAGE:// 存储设备
                        usbDeviceList.add(device);
                        break;
                    case UsbConstants.USB_CLASS_HID:// HID 设备，如键盘、鼠标
                        break;
                    case UsbConstants.USB_CLASS_PRINTER:// 打印机
                        break;
                    case UsbConstants.USB_CLASS_STILL_IMAGE:// 摄像头
                        break;
                    default:
                        break;
                }
            }
        }
        return usbDeviceList;
    }

    public static UsbDeviceConnection openDevice(Context context,UsbDevice usbDevice){
        UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        return usbManager.openDevice(usbDevice);
    }

//    public static void getUsbStorageInfo(Context context, UsbDeviceConnection connection) {
//        UsbStorageAccess storageAccess = new UsbStorageAccess(connection);
//        StorageVolume[] storageVolumes = storageAccess.getVolumeList();
//
//        for (StorageVolume volume : storageVolumes) {
//            // 这里可以获取存储设备的路径、状态等信息
//            String path = volume.getPath();
//            // 处理存储设备信息
//        }
//    }
//
//    public static void requestAndGetUsbStorageInfo(Context context, UsbDevice device) {
//        UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
//        if (usbManager.hasPermission(device)) {
//            UsbDeviceConnection connection = usbManager.openDevice(device);
//            if (connection != null) {
//                try {
//                    getUsbStorageInfo(context, connection);
//                } finally {
//                    connection.close();
//                }
//            }
//        }
//    }

//    public void readUsbFiles(Context context) {
//        UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
//        HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
//
//        for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
//            UsbDevice usbDevice = entry.getValue();
//            if (usbManager.hasPermission(usbDevice)) {
//                // 如果已经有权限，直接连接USB设备
//                UsbDeviceConnection connection = usbManager.openDevice(usbDevice);
//                if (connection != null) {
//                    try {
//                        // 获取U盘的存储接入点
//                        String path = UsbStorageAccess.getUsbDrivePath(usbDevice, connection);
//                        if (path != null) {
//                            // 读取U盘路径下的文件列表
//                            File usbDrive = new File(path);
//                            File[] files = usbDrive.listFiles();
//                            // 处理文件列表
//                            if (files != null) {
//                                for (File file : files) {
//                                    // 你的文件处理逻辑
//                                }
//                            }
//                        }
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    } finally {
//                        // 确保在结束时关闭连接
//                        connection.close();
//                    }
//                }
//            } else {
//                // 请求权限
//                usbManager.requestPermission(usbDevice, pendingIntent);
//            }
//        }
//    }
}
